package com.letfox.auth.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.letfox.common.enums.ValidateEnum;
import com.letfox.common.model.ResultResponse;
import com.letfox.common.util.JacksonUtil;
import com.letfox.common.util.http.HTTPResponseUtil;

/**
 * Spring全局拦截器
 */
public class Interceptor extends HandlerInterceptorAdapter {
	private static final Logger logger = LoggerFactory.getLogger(Interceptor.class);

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		String servletPath = request.getRequestURL().toString();
		
		//登录
		if(servletPath.contains("/auth/login")){
			String account = request.getParameter("account");
			String password = request.getParameter("password");
			if(StringUtils.isBlank(account)){
				ResultResponse resultResponse = new ResultResponse();
				resultResponse.setStatusCode(ValidateEnum.V107.getCode());
				resultResponse.setStatusMessage(ValidateEnum.V107.getMessage());
				HTTPResponseUtil.renderText(response, JacksonUtil.toJson(resultResponse));
				return false;
			}else if(StringUtils.isBlank(password)){
				ResultResponse resultResponse = new ResultResponse();
				resultResponse.setStatusCode(ValidateEnum.V108.getCode());
				resultResponse.setStatusMessage(ValidateEnum.V108.getMessage());
				HTTPResponseUtil.renderText(response, JacksonUtil.toJson(resultResponse));
				return false;
			}
			return true;
		}
		
		//注册
		if(servletPath.contains("/auth/register")){
			String account = request.getParameter("account");
			String password = request.getParameter("password");
			String mobilePhone = request.getParameter("mobilePhone");
			
			ResultResponse resultResponse = null;
			if(StringUtils.isBlank(account)){
				resultResponse = new ResultResponse();
				resultResponse.setStatusCode(ValidateEnum.V107.getCode());
				resultResponse.setStatusMessage(ValidateEnum.V107.getMessage());
				HTTPResponseUtil.renderText(response, JacksonUtil.toJson(resultResponse));
				return false;
			}else if(StringUtils.isBlank(password)){
				resultResponse = new ResultResponse();
				resultResponse.setStatusCode(ValidateEnum.V108.getCode());
				resultResponse.setStatusMessage(ValidateEnum.V108.getMessage());
				HTTPResponseUtil.renderText(response, JacksonUtil.toJson(resultResponse));
				return false;
			}else if(StringUtils.isBlank(mobilePhone)){
				resultResponse = new ResultResponse();
				resultResponse.setStatusCode(ValidateEnum.V109.getCode());
				resultResponse.setStatusMessage(ValidateEnum.V109.getMessage());
				HTTPResponseUtil.renderText(response, JacksonUtil.toJson(resultResponse));
				return false;
			}
			return true;
		}
		
		return super.preHandle(request, response, handler);
	}
	

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

}
